package dao;

/*Pedro teste*/

import java.util.ArrayList;

import modelo.Cliente;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DaoAdim {
	private SessionFactory fabrica;

	public DaoAdim()throws Exception
	{	fabrica=new Configuration().addClass(modelo.Cliente.class).buildSessionFactory();
		
	}
	public Cliente consultar(Cliente c)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		c=(Cliente)sessao.get(modelo.Cliente.class,c.getNome_cli());
		
		transacao.commit();
		sessao.flush();
		sessao.close();
		return c;
	}
	public void excluir(Cliente c)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.delete(c);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public void alterar(Cliente c)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.update(c);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public ArrayList consultarTodos(){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cliente");
		ArrayList clientes=(ArrayList)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return clientes;
	}
	public Cliente consultarPrimeiro(Cliente c){			
		Cliente cli = null;
		//JOptionPane.showMessageDialog(null,String.valueOf("pedro"));
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cliente " + "where id_cliente = 1");
		cli = (Cliente) q.uniqueResult();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return cli;
	}
	
	public Cliente consultarProximo(int idCliente)throws Exception
	{
		Cliente cli = null;		
		//JOptionPane.showMessageDialog(null,String.valueOf(p.getId_plano()));		
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cliente " + "where id_cliente = " + idCliente);
		cli = (Cliente) q.uniqueResult();		
		transacao.commit();
		sessao.flush();
		sessao.close();	
		return cli;
	}
	
	public Cliente consultarUltimo(Cliente c){		
		Cliente cli = null;			
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cliente " + "WHERE id_cliente = (SELECT MAX(id_cliente) FROM modelo.Cliente)");
		cli = (Cliente) q.uniqueResult();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return cli;
	}

	
	

}

